﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Project.Module.DAO.BL;
using Project.Module.DAO.IBL;
using Project.Module.Model;
using Project.Module.Model.CMS;
using Project.Engine.Utilities;

namespace Project.Module.DAO.CMSBL
{
    public class RecipeBL : IBLTable<Recipe, int>
    {
        public List<Model.CMS.RecipeModel> GetList()
        {
            var query = from e in this.Context.Recipe
                        orderby e.CreatedDate descending
                        select new RecipeModel()
                        {
                            RecipeID = e.RecipeID,
                            CreatedBy = e.CreatedBy.Value,
                            ModifiedBy = e.ModifiedBy.Value,
                            Title = e.Title,
                            Description = e.Description,
                            CreatedDate = e.CreatedDate != null ? e.CreatedDate.Value : Convert.ToDateTime("01/01/1900"),
                            ModifiedDate = e.ModifiedDate != null ? e.ModifiedDate.Value : Convert.ToDateTime("01/01/1900"),
                            ProductID = e.ProductID != null ? e.ProductID.Value : 0,
                        };

            return query.Take(50).ToList();
        }

        public void AddRecipes(string title, string content, string status)
        {
            DateTime date = DateTime.Now;
            title = StringUtil.ISO8859ToUnicode(title);
            content = StringUtil.ISO8859ToUnicode(content);

            int check = 0;
            if (status == "true")
            {
                check = 1;
            }

            string sql = @"insert into News (
                              title, content, date, status
                           ) values (
                              {0}, {1}, {2}, {3}
                           )";
            var query = this.Context.ExecuteQuery<CustomerModel>(sql, new object[] { title, content, date, check }).ToList();
        }

        public void DeleteRecipe(int id)
        {
            New news = this.Context.News.Single(c => c.id == id);
            this.Context.News.DeleteOnSubmit(news);
            this.Context.SubmitChanges();
        }

        public void EditRecipe(int id, string title, int productID, string description, int modifiedBy)
        {
            DateTime date = DateTime.Now;
            title = StringUtil.ISO8859ToUnicode(title);
            description = StringUtil.ISO8859ToUnicode(description);

            string sql = @"update Recipe 
                           set title = {0},productID={1} , description = {2}, 
                               modifiedDate = {3}, modifiedBy = {4}
                           where id = {4}";
            var query = this.Context.ExecuteQuery<NewsModel>(sql, new object[] { title, productID, description, date, modifiedBy, id }).ToList();
        }

    }
}
